<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addrol"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="isedit(row)">编辑</el-button>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="querypatams.page"
                :page-sizes="[3, 5, 10, 15]"
                :page-size="querypatams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗实现修改编辑功能 -->
      <!-- 新增弹框 -->
      <el-dialog
        :title=" isEdit ? '编辑' : '新增' "
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getRoleList, addRole, updataRole } from '@/api/settings'
export default {
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      isEdit: false, // true为编辑窗口 false为新增窗口
      showDialog: false,
      total: 0,
      querypatams: {
        page: 1, // 页码
        pagesize: 3 // 每页条数
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '请输入角色名字', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    // 实现编辑修改功能
    async doEdit() {
      try {
        const res = await updataRole(this.roleForm)
        console.log(res)
        this.loadRoleList()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 显示编辑弹窗
    isedit(data) {
      this.showDialog = true // 打开弹窗
      this.isEdit = true // 打开编辑窗口
      this.roleForm = { ...data }
    },
    // 实现添加角色
    async hAdd() {
      try {
        const res = await addRole(this.roleForm)
        // 把数据显示到最前面
        this.querypatams.page = Math.ceil(this.total / this.querypatams.pagesize) // 得到共多少页
        this.loadRoleList() // 渲染表格
        this.showDialog = false // 关闭弹窗
        this.$message.success(res.message)
      } catch (error) {
        console.log(error)
      }
    },
    // 提交新增或者编辑修改的数据
    hSubmit() {
      // 兜底校验
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return this.$message.error('验证失败')
        this.isEdit ? this.doEdit() : this.hAdd()
      })
    },
    // 打开新增弹窗
    addrol() {
      this.showDialog = true
      this.isEdit = false
    },
    // 获取所有角色列表
    async loadRoleList() {
      try {
        const res = await getRoleList(this.querypatams)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 每页显示几条
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.querypatams.pagesize = val
      this.loadRoleList()
    },
    // 当前的页码
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.querypatams.page = val
      this.loadRoleList()
    }
  }
}
</script>
